﻿namespace BTS.COMMON
{
    using System;
    using System.IO;
    using System.Net;
    using System.Text;
    using System.Web;

    public class HttpHelper
    {
        private StringBuilder urlParm = new StringBuilder();

        public void AddUrlParm(string key, string value)
        {
            if (!string.IsNullOrEmpty(key) && !string.IsNullOrEmpty(value))
            {
                value = HttpContext.Current.Server.UrlEncode(value);
                if (this.urlParm.Length > 0)
                {
                    this.urlParm.Append("&").Append(key).Append("=").Append(value);
                }
                else
                {
                    this.urlParm.Append(key).Append("=").Append(value);
                }
            }
        }

        public static void CheckUrlReferrer(string pageUrl)
        {
            if (string.IsNullOrEmpty(pageUrl))
            {
                pageUrl = string.Empty;
            }
            string url = "http://" + HttpContext.Current.Request.Url.Host + FileHelper.AppVirRoot + pageUrl;
            if (HttpContext.Current.Request.UrlReferrer == null)
            {
                HttpContext.Current.Response.Redirect(url);
                HttpContext.Current.Response.End();
            }
            if (!HttpContext.Current.Request.UrlReferrer.ToString().ToLower().StartsWith(url.ToLower()))
            {
                HttpContext.Current.Response.Redirect(url);
                HttpContext.Current.Response.End();
            }
        }

        public string RequestResult(string url, string method, string encoding)
        {
            try
            {
                StreamReader reader;
                string str;
                if (method.ToLower() == "post")
                {
                    HttpWebRequest request = (HttpWebRequest) WebRequest.Create(url);
                    byte[] bytes = Encoding.ASCII.GetBytes(this.urlParm.ToString());
                    request.Method = method;
                    request.ContentType = "application/x-www-form-urlencoded";
                    request.ContentLength = bytes.Length;
                    Stream requestStream = request.GetRequestStream();
                    requestStream.Write(bytes, 0, bytes.Length);
                    requestStream.Close();
                    HttpWebResponse response = (HttpWebResponse) request.GetResponse();
                    reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding(encoding));
                    str = reader.ReadToEnd();
                    reader.Close();
                    response.Close();
                    return str;
                }
                if (method.ToLower() == "get")
                {
                    Stream stream = new WebClient().OpenRead(url);
                    reader = new StreamReader(stream, Encoding.GetEncoding(encoding));
                    str = reader.ReadToEnd();
                    reader.Close();
                    stream.Close();
                    return str;
                }
                return "Request Error! no method";
            }
            catch (Exception exception)
            {
                return ("Request Error!" + exception.Message);
            }
        }

        public static string PageUrl
        {
            get
            {
                if (HttpContext.Current.Request.Url.Port == 80)
                {
                    return ("http://" + HttpContext.Current.Request.Url.Host + HttpContext.Current.Request.Path);
                }
                else
                {
                    return ("http://" + HttpContext.Current.Request.Url.Host + ":" + HttpContext.Current.Request.Url.Port + HttpContext.Current.Request.Path);
                }
            }
        }

        public string UrlParm
        {
            get
            {
                return this.urlParm.ToString();
            }
        }
    }
}

